package com.fy.module.sys.controller;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.fy.module.sys.entity.SysCode;
import com.fy.module.sys.service.ICodeService;
import com.fy.utils.VerifyCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * 江迪
 */
@Controller
public class VerifyController {

    @Autowired
    private ICodeService codeService;

    @RequestMapping("/verify")
    public void getVerifyCode(HttpServletResponse response) throws IOException {//创建验证码VerifyCode对象
        VerifyCode vc = new VerifyCode();
        //获取图片对象
        BufferedImage bi = vc.getImage();
        //获得图片的文本内容
        String text = vc.getText();

        SysCode sysCode=new SysCode();
        sysCode.setCodeId(IdWorker.getIdStr());
        sysCode.setCodeName(text);
        //将生成的验证码加入到数据库
        System.out.println(sysCode);
        codeService.addVerifyCode(sysCode);

//		//将系统生成的文本内容保存到session中
//		request.getSession().setAttribute("text", text);
        //向浏览器输出图片  ImageIO类用于读写图片
        ImageIO.write(bi, "JPEG", response.getOutputStream());}
}
